const formatTime = value => {
  var date = new Date(value);
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D =  (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
  var df = Y + M + D;
  return df;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

/**
 * post 方法，接受params参数对象
 */
function post(url, params) {
  let promise = new Promise(function(resolve, reject) {
    wx.showLoading({
      title: '数据加载中...',
    })
    wx.request({
      url: `${getApp().globalData.host}${url}`,
      data: params,
      method: 'POST',
      header:{
        Authorization: wx.getStorageSync('token') || '',
      },
      success: function(res) {
        if(res.data.code == 401){
          // reject('请求失败，请重试！')
          wx.setStorageSync('versions', '')
          wx.clearStorageSync('token')
          wx.clearStorageSync('userInfo')
          wx.navigateTo({
            url: '/pages/index/index',
          })
        }else if(res.data.code == 500){
          wx.showToast({ title: res.data.msg, icon: 'none' });
        }else{
          resolve(res)
        }
      },
      fail: function(res) {
        reject(res)
      },
      // 无论获取数据是否成功都会执行该方法
      complete:()=>{
        wx.hideLoading() // 关闭loading
      }
    })
  });
  return promise;
}

/**
 * get 方法，接受params参数对象
 */
function get(url, params, type) {
  let promise = new Promise(function(resolve, reject) {
    let link = ''
    if(params){
      link = `${getApp().globalData.host}${url}${'/' + params}`
    }else{
      link = `${getApp().globalData.host}${url}`
    }
    wx.showLoading({
      title: '数据加载中...',
    })
    wx.request({
      url: link,
      // data: params,
      method: 'GET',
      header:{
        Authorization: wx.getStorageSync('token') || '',
      },
      success: function(res) {
        if(res.data.code == 401){
          // reject('请求失败，请重试！')
          wx.setStorageSync('versions', '')
          wx.clearStorageSync('token')
          wx.clearStorageSync('userInfo')
          wx.navigateTo({
            url: '/pages/index/index',
          })
        }else if(res.data.code == 500){
          wx.showToast({ title: res.data.msg, icon: 'none' });
          if(type){
            resolve(res)
          }
        }else{
          resolve(res)
        }
      },
      fail: function(res) {
        reject(res)
      },
      // 无论获取数据是否成功都会执行该方法
      complete:()=>{
        wx.hideLoading() // 关闭loading
      }
    })
  });
  return promise;
}

/**
 * put 方法，接受params参数对象
 */
function put(url, params) {
  let promise = new Promise(function(resolve, reject) {
    wx.showLoading({
      title: '数据加载中...',
    })
    wx.request({
      url: `${getApp().globalData.host}${url}`,
      data: params,
      method: 'PUT',
      header:{
        Authorization: wx.getStorageSync('token') || '',
      },
      success: function(res) {
        if(res.data.code == 401){
          // reject('请求失败，请重试！')
          wx.setStorageSync('versions', '')
          wx.clearStorageSync('token')
          wx.clearStorageSync('userInfo')
          wx.navigateTo({
            url: '/pages/index/index',
          })
        }else if(res.data.code == 500){
          wx.showToast({ title: res.data.msg, icon: 'none' });
        }else{
          resolve(res)
        }
      },
      fail: function(res) {
        reject(res)
      },
      // 无论获取数据是否成功都会执行该方法
      complete:()=>{
        wx.hideLoading() // 关闭loading
      }
    })
  });
  return promise;
}


/**
 * get 方法，接受params参数对象
 */
function getP(url, params, type) {
  let promise = new Promise(function(resolve, reject) {
    wx.showLoading({
      title: '数据加载中...',
    })
    wx.request({
      url: `${getApp().globalData.host}${url}`,
      data: params,
      method: 'GET',
      header:{
        Authorization: wx.getStorageSync('token') || '',
      },
      success: function(res) {
        if(res.data.code == 401){
          // reject('请求失败，请重试！')
          wx.setStorageSync('versions', '')
          wx.clearStorageSync('token')
          wx.clearStorageSync('userInfo')
          wx.navigateTo({
            url: '/pages/index/index',
          })
        }else if(res.data.code == 500){
          wx.showToast({ title: res.data.msg, icon: 'none' });
          if(type){
            resolve(res)
          }
        }else{
          resolve(res)
        }
      },
      fail: function(res) {
        reject(res)
      },
      // 无论获取数据是否成功都会执行该方法
      complete:()=>{
        wx.hideLoading() // 关闭loading
      }
    })
  });
  return promise;
}

module.exports = {
  formatTime,
  post,
  get,
  getP,
  put
}
